package obth;

import java.util.concurrent.BlockingQueue;

public class ReqResThread implements Runnable {
    private BlockingQueue<ReqRes> topic_req;
    private BlockingQueue<PageEvent> topic_event;

    public ReqResThread(BlockingQueue<ReqRes> topic_req, BlockingQueue<PageEvent> topic_event) {

        this.topic_req = topic_req;
        this.topic_event = topic_event;
    }

    @Override
    public void run() {
        while (true) {
            ReqRes reqRes = null;
            try {
                reqRes = topic_req.take();
            } catch (InterruptedException e) {
                reqRes = new ReqRes();
            }
            Tool.debug(reqRes);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            reqRes.setDataToRecv(reqRes.getDataToSend().toUpperCase());
            ReqResPageEvent reqResPageEvent = new ReqResPageEvent(System.currentTimeMillis(), reqRes);

            try {
                topic_event.put(reqResPageEvent);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
